package com.dongdong.controller;

import com.dongdong.em.Result;
import com.dongdong.em.ResultEnum;
import com.dongdong.service.CartService;
import com.dongdong.util.JWTUtil;
import com.dongdong.vo.CartVO;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("cart")
@RefreshScope   //热更新
public class CartController {

    @Autowired
    private JWTUtil jwtUtil;

    @Autowired
    private CartService cartService;

    @GetMapping("AddCart")//@RequestHeader  参数条件 必须是英文 才能传递
    public Result AddCart(@RequestHeader String Autherizetion,@RequestParam Integer num,@RequestParam Long skuid){

        Claims claims = jwtUtil.parseJwt(Autherizetion);//解析token
        String userid = claims.getId();

        cartService.AddCart(userid,num,skuid);

        return Result.ZHRESPONSE(ResultEnum.ADDCART_SUCCESS);//成功加入购物车
    }

    @GetMapping("getCart")
    public Result GetCart(@RequestHeader String Autherizetion){
        Claims claims = jwtUtil.parseJwt(Autherizetion);
        String userid = claims.getId();
        List<CartVO> cartVOList = cartService.getCart(userid);
        return Result.OK().setData(cartVOList);
    }
}
